package com.migu.spms.service.impl;

import java.util.List;
import java.util.TreeSet;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.migu.spms.constant.Constant;
import com.migu.spms.dao.entity.UserProjectCriteria;
import com.migu.spms.dao.entity.UserProjectDO;
import com.migu.spms.dao.inf.IUserProjectDO;
import com.migu.spms.service.data.User;
import com.migu.spms.service.inf.IUserRoleControlService;
import com.migu.spms.webapi.data.ButtonInfo;
import com.migu.spms.webapi.data.ButtonResponse;
@Service("IUserRoleControlServiceImpl")
public class UserRoleControlServiceImpl implements IUserRoleControlService {
	@Resource(name="IUserProjectDO")
	private IUserProjectDO userProjuectDAO;
	
	@Override
	public ButtonResponse userRoleControl(User user,Long projectId) throws Exception {
		ButtonResponse buttonResponse = new ButtonResponse();
		ButtonInfo buttonInfo = new ButtonInfo();
		TreeSet<String> buttonTree = new TreeSet<String>();  
		//权限控制,查询用户表(userId和pid)
		Long userId = (long) user.getUserId();
		UserProjectCriteria userInfo = new UserProjectCriteria();
		userInfo.setUid(Integer.valueOf(userId.toString()));
		userInfo.setPid(Integer.valueOf(projectId.toString()));
		List<UserProjectDO> userInfoList = userProjuectDAO.selectListByCriteria(userInfo);
		if(userInfoList != null){
			for (UserProjectDO userInfo_01 : userInfoList) {
				Long roleId = Long.valueOf(userInfo_01.getRid().toString());
				//添加按钮数组
				if(roleId.equals(Constant.Role.DEVELOP_MANAGER) || roleId.equals(Constant.Role.PRODUCT_MANAGER)){
					//项目经理（开发经理或产品经理）
					buttonTree.add(Constant.AccessControl.EXPORT_IN);
					buttonTree.add(Constant.AccessControl.EXPORT_OUT);
					buttonTree.add(Constant.AccessControl.CREATE_NEW_REQUIRE);
					buttonTree.add(Constant.AccessControl.BASELINE_VERSION);
					buttonTree.add(Constant.AccessControl.MOVE_REQUIRE);
					buttonTree.add(Constant.AccessControl.UPDATE_BASELINE_VERSION);
					
					buttonTree.add(Constant.AccessControl.UPDATE_SCHEDUL_INFO);
					buttonTree.add(Constant.AccessControl.UPDATE_TEST_SCHEDUL_INFO);//测试排期 since 2017-08-01
				
				}else if(roleId.equals(Constant.Role.TEST_MANAGER)){
					//测试经理
					buttonTree.add(Constant.AccessControl.ADD_COMMENTS);
					buttonTree.add(Constant.AccessControl.UPDATE_TEST_SCHEDUL_INFO);//测试排期 since 2017-08-01
					buttonTree.add(Constant.AccessControl.CREATE_NEW_REQUIRE);
				}else{
					//其他人员
					buttonTree.add(Constant.AccessControl.ADD_COMMENTS);
					buttonTree.add(Constant.AccessControl.CREATE_NEW_REQUIRE);
				}
			}
			if(buttonTree.size()>0){
				String[] buttons = buttonTree.toArray(new String[buttonTree.size()]);
				buttonInfo.setButtons(buttons);
			}
		}
		buttonResponse.setButtonInfo(buttonInfo);
		return buttonResponse;
	}
}
